package com.wscx.jingjie.util;

import com.wscx.jingjie.entity.User;
import com.wscx.jingjie.jwt.Jwt;
import com.wscx.jingjie.service.UserService;

import javax.servlet.http.HttpServletRequest;

/**
 * 获取用户相关信息工具
 */
public class UserUtils {

//    private static UserService userService = SpringContextHolder.getBean(UserService.class);

    /**
     * 获取当前请求中的token
     *
     * @return
     */
    public static String getToken() {
        String token = null;
        HttpServletRequest request = ServletUtils.getRequest();
        if (null != request) {
            token = request.getHeader("token");
        }
        return token;
    }

    /**
     * 获取用户id
     *
     * @return
     */
    public static int getUserId() {
        return Integer.parseInt(Jwt.getUserId(getToken()));
    }


    /**
     * 根据ID获取用户
     *
     * @param id
     * @return 取不到返回null
     */
   /* public static SysUser get(String id) {
        SysUser user = (SysUser) UserUtils.getCache(USER_CACHE_ID_ + id);
        //TODO:先取消用户缓存，以后再定
        user = userService.getObjectById(id);
        if (user == null) {
            return null;
        }
        user.setRoles(roleService.getListByUser(user));
        user.setMenus(menuService.getMenuTreeByUser(user));
        UserUtils.putCache(USER_CACHE_ID_ + user.getId(), user);
        UserUtils.putCache(USER_CACHE_LOGIN_NAME_ + user.getLoginName(), user);
        return user;
    }*/

    /**
     * 获取当前用户
     *
     * @return 取不到返回 new User()
     */
//    public static User getUser() {
//        User user = userService.getObjectById(UserUtils.getUserId());
//        if (user != null) {
//            return user;
//        }
//        // 如果没有登录，则返回实例化空的User对象。
//        return new User();
//    }


}
